//
//  FBBugViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/2.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "FBBugViewController.h"
#import "FBPayOrderViewController.h"
#import "FBBugModel.h"

@interface FBBugViewController ()

@property (nonatomic,strong)UITextField * textField1;
@property (nonatomic,strong)UITextField * textField2;
@property (nonatomic,strong)UIView * bgView;

@end

@implementation FBBugViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setProperty];
    [self createUI];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    
}

-(void)createUI{
    
    UIView * bgView = [[UIView alloc]init];
    [self.view addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kStatusBarAndNavigationBarHeight);
        make.right.left.bottom.equalTo(self.view);
    }];
    _bgView = bgView;
    
    FBTradeModel * FBmodel = [FBTradeModel sharedFBTradeModel];
    UILabel * lab1 = [[UILabel alloc]init];
    [self setLab:lab1 withColor:RGB(30, 63, 89) andFont:28 andText:[NSString stringWithFormat:@"购买 %@",FBmodel.selectedHeaderModel.symbol] andTextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(10);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(30);
    }];
    
    
    UILabel * lab2 = [[UILabel alloc]init];
    [self setLab:lab2 withColor:RGB(105, 154, 219) andFont:18 andText:[NSString stringWithFormat:@"%@ CNY/%@",_dataSource.price,FBmodel.selectedHeaderModel.symbol] andTextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(0);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(30);
    }];
    
    UILabel * lab3 = [[UILabel alloc]init];
    [self setLab:lab3 withColor:RGB(152, 169, 182) andFont:12 andText:[NSString stringWithFormat:@"限额 %@-%@ CNY",_dataSource.min_limit,_dataSource.max_limit] andTextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:lab3];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab2.mas_bottom).offset(0);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(30);
    }];
   
    int i = 0;
    for (NSString * temp in _dataSource.payment) {
        NSString * payname = @"";
        NSString * payImgName = @"";
        //支付宝
        if ([temp integerValue]==4) {
            payname= @"支付宝";
            payImgName=@"支付宝";
            //微信
        }else if ([temp integerValue]==5){
            payname= @"微信";
            payImgName=@"微信-3";
            //银联
        }else if ([temp integerValue]==6){
            payname= @"银行卡";
            payImgName=@"银行卡";
        }
        UIImageView * alipay = [[UIImageView alloc]init];
        [bgView addSubview:alipay];
        alipay.image = [UIImage imageNamed:payImgName];
        [alipay mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView.mas_left).offset(15+i*75);
            make.top.equalTo(lab3.mas_bottom).offset(10);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(18);
        }];
        
        UILabel * lab = [[UILabel alloc]init];
        [self setLab:lab withColor:RGB(30, 63, 89) andFont:12 andText:payname andTextAlignment:NSTextAlignmentLeft];
        [bgView addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(alipay.mas_right).offset(5);
            make.centerY.equalTo(alipay.mas_centerY).offset(0);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(80);
        }];
        i++;
      
    }
    
    
    UIView * view = [[UIView alloc]init];
    [bgView addSubview:view];
    view.backgroundColor= [UIColor whiteColor];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(15);
        make.right.equalTo(bgView.mas_right).offset(-15);
        make.height.mas_equalTo(100);
        make.top.equalTo(lab3.mas_bottom).offset(80);
    }];
    view.layer.borderWidth=1;
    view.layer.borderColor=RGB(200, 207, 213).CGColor;
    
    UITextField * textField1=[[UITextField alloc]init];
    [view addSubview:textField1];
    [self setTextField:textField1 WithPlaceholder:@"" and:UIKeyboardTypeDecimalPad];
    textField1.backgroundColor = [UIColor clearColor];
    [textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(25);
        make.right.equalTo(view.mas_right).offset(-120);
        make.height.mas_equalTo(50);
        make.top.equalTo(view.mas_top).offset(0);
    }];
    _textField1=textField1;
    UIButton *  bugAll = [[UIButton alloc]init];
    [view addSubview:bugAll];
    [bugAll setTitleColor:ThemeColor forState:UIControlStateNormal];
    [bugAll setTitle:[NSString stringWithFormat:@"全部买入"] forState:UIControlStateNormal];
    bugAll.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    bugAll.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [bugAll addTarget:self action:@selector(buyAllClick) forControlEvents:UIControlEventTouchUpInside];
//    bugAll.backgroundColor=RGB(235, 240, 248);
    [bugAll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(0);
        make.right.equalTo(view.mas_right).offset(-10);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(50);
    }];
    
    
    UIView * line1 = [[UIView alloc]init];
    [view addSubview:line1];
    line1.backgroundColor=RGB(200, 207, 213);
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bugAll.mas_left).offset(0);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(bugAll.mas_centerY).offset(0);
    }];
    /*
     * CNY
     */
    UILabel * CNY = [[UILabel alloc]init];
    [self setLab:CNY withColor:RGB(40, 81, 109) andFont:15 andText:@"CNY" andTextAlignment:NSTextAlignmentRight];
    [view addSubview:CNY];
    [CNY mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line1.mas_left).offset(0);
        make.centerY.equalTo(bugAll.mas_centerY).offset(0);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(50);
    }];
    
    UIView * line = [[UIView alloc]init];
    [view addSubview:line];
    line.backgroundColor=RGB(200, 207, 213);
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(25);
        make.right.equalTo(view.mas_right).offset(-25);
        make.height.mas_equalTo(1);
        make.top.equalTo(view.mas_top).offset(50);
    }];
    
    UITextField * textField2=[[UITextField alloc]init];
    [view addSubview:textField2];
    textField2.userInteractionEnabled = NO;
    [self setTextField:textField2 WithPlaceholder:@"" and:UIKeyboardTypeDecimalPad];
    textField2.backgroundColor = [UIColor clearColor];
    [textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(25);
        make.right.equalTo(view.mas_right).offset(-120);
        make.height.mas_equalTo(50);
        make.top.equalTo(view.mas_top).offset(50);
    }];
    _textField2=textField2;
    
    UILabel * BTC = [[UILabel alloc]init];
    [self setLab:BTC withColor:RGB(40, 81, 109) andFont:15 andText:FBmodel.selectedHeaderModel.symbol andTextAlignment:NSTextAlignmentRight];
    [view addSubview:BTC];
    [BTC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right).offset(-20);
        make.centerY.equalTo(textField2.mas_centerY).offset(0);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(80);
    }];
    
    
    UILabel * labtip = [[UILabel alloc]init];
    [self setLab:labtip withColor:RGB(157, 175, 187) andFont:12 andText:@"该订单需要您在15分钟之内支付" andTextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:labtip];
    [labtip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(15);
        make.top.equalTo(view.mas_bottom).offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(300);
    }];
    
    
    
    UIButton *  btn1 = [[UIButton alloc]init];
    [bgView addSubview:btn1];
    [btn1 setTitleColor:ThemeColor forState:UIControlStateNormal];
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    btn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn1.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [btn1 addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor=RGB(235, 240, 248);
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(40);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.width.mas_equalTo((G_SCREENWIDTH-30-20)/2.0);
        make.height.mas_equalTo(45);
    }];
    
    
    UIButton *  btn2 = [[UIButton alloc]init];
    [bgView addSubview:btn2];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"下单" forState:UIControlStateNormal];
    btn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn2.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [btn2 addTarget:self action:@selector(orderClick) forControlEvents:UIControlEventTouchUpInside];
    //    [btn1.layer setBorderWidth:1.0];
    //    btn1.layer.borderColor=ThemeColor.CGColor;
    btn2.backgroundColor=RGB(99, 140, 220);
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(40);
        make.right.equalTo(bgView.mas_right).offset(-15);
        make.width.mas_equalTo((G_SCREENWIDTH-30-20)/2.0);
        make.height.mas_equalTo(45);
    }];


    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [self.view  addGestureRecognizer:singleTapGesture];
    
}

#pragma mark - gesture actions
- (void)closeKeyboard:(UITapGestureRecognizer *)recognizer {
    [self.view endEditing:YES];

    double text1 = [_textField1.text doubleValue];
    double text2 = text1 / [_dataSource.price doubleValue];
    _textField2.text = MCDOUBLE_STR(text2);
    
}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    //    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
 
    if (textField == _textField1) {
        if ([textField.text doubleValue]>[_dataSource.max_limit doubleValue]) {
            textField.text =MCDOUBLE_STR([_dataSource.max_limit doubleValue]);
        }
        double text1 = [_textField1.text doubleValue];
        double text2 = text1 / [_dataSource.price doubleValue];
        _textField2.text = MCDOUBLE_STR(text2);
    }
}

#pragma mark-取消
-(void)cancelClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark-下单
-(void)orderClick{
    
    
    /*
     * 创建订单
     */
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/generateOrder",DEFAULTSEVERBASEURL];
    
    
    NSDictionary * Dic  = @{
                            @"bill_sn":_dataSource.bill_sn,//广告sn
                            @"amount":_textField2.text,//购买金额（法币）
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            [HUD showMessage:dic[@"msg"]];
            
//            order_sn    字符串    订单号
//            user_id    整形    下单人id
//            bill_user_id    整形    广告发布者id
            NSDictionary * dicc = dic[@"data"];

            
            FBBugModel * BModel = [FBBugModel mj_objectWithKeyValues:dicc];
            
            
            FBPayOrderViewController * VC = [[FBPayOrderViewController alloc]init];
            VC.order_sn = BModel.order_sn;
            [weakSelf.navigationController pushViewController:VC animated:YES];
            
        }else{
            [HUD showMessage:dic[@"msg"]];
            
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
        [HUD dismissInView:weakSelf.bgView];

    }];
    
    
   
}

#pragma mark-全部买入
-(void)buyAllClick{
    
    _textField1.text = _dataSource.max_limit;
    double text1 = [_textField1.text doubleValue];
    double text2 = text1 / [_dataSource.price doubleValue];
    _textField2.text = MCDOUBLE_STR(text2);
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
